package io.renren.modules.echarts.service.Impl;

import io.renren.modules.echarts.dao.TeacherEvaluationChartsDao;
import io.renren.modules.echarts.entity.*;
import io.renren.modules.echarts.service.TeacherEvaluationChartsService;
import io.renren.modules.echarts.util.DateFormat;
import io.renren.modules.jxgk.dao.TeacherEvaluationSumDao;
import io.renren.modules.jxgk.dao.TeacherInfoDao;
import io.renren.modules.jxgk.entity.TeacherCourseContentEntity;
import io.renren.modules.jxgk.entity.TeacherEvaluationSumEntity;
import io.renren.modules.jxgkphone.dao.TeachingEvaluationMapper;
import io.renren.modules.jxgkphone.entity.EvaluationContent;
import io.renren.modules.jxgkphone.entity.EvaluationLevel;
import io.renren.modules.jxgkphone.entity.EvaluationType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class TeacherEvaluationChartsServiceImpl implements TeacherEvaluationChartsService{
    @Autowired
    TeacherEvaluationSumDao teacherEvaluationSumDao;
    @Autowired
    TeachingEvaluationMapper teachingEvaluationMapper;
    @Autowired
    TeacherEvaluationChartsDao teacherEvaluationChartsDao;
    @Autowired
    TeacherInfoDao teacherInfoDao;
    @Autowired
    TeacherPortraitServiceImpl teacherPortraitService;
    @Override
    public List<TeacherEvaluationSumEntity> queryList(Map<String, Object> map){
        return teacherEvaluationSumDao.queryList(map);
    }
    @Override
    public int queryTotal(Map<String, Object> map){
        return  teacherEvaluationSumDao.queryTotal(map);
    }
    @Override
    public void deleteBatch(String[] termIds){
        teacherEvaluationSumDao.deleteBatch(termIds);
    }
    @Override
    public void save(TeacherEvaluationSumEntity ste){
        teacherEvaluationSumDao.save(ste);
    }
    @Override
    public TeacherEvaluationSumEntity queryObject(String index){
        return teacherEvaluationSumDao.queryObject(index);
    }
    @Override
    public void update(TeacherEvaluationSumEntity ste){
        teacherEvaluationSumDao.update(ste);
    }
    @Override
    public Object findAllEvaluationContentMap(Map<String, Object> map) {
        List<EvaluationContent> evaluationContentList=teacherEvaluationSumDao.findAllEvaluationContentList(map);
        List<EvaluationType> evaluationTypeList = teachingEvaluationMapper.findAllEvaluationType();
        List<EvaluationLevel> evaluationLevelList=teachingEvaluationMapper.findAllEvaluationLevel();
        for(EvaluationType evaluationType:evaluationTypeList)
        {
            List<EvaluationLevel> evaluationLevels=new ArrayList<>();
            for(EvaluationLevel evaluationLevel:evaluationLevelList)
            {
                if(evaluationLevel.getEvaluationTypeId().equals(evaluationType.getEvaluationTypeId()))
                {
                    List<EvaluationContent> evaluationContents=new ArrayList<>();
                    for(EvaluationContent evaluationContent:evaluationContentList)
                    {
                        if(evaluationContent.getEvaluationLevelId().equals(evaluationLevel.getEvaluationLevelId()))
                        {
                            evaluationContents.add(evaluationContent);
                            if(evaluationContent.isChecked())
                            {
                                evaluationLevel.setLevel(true);
                            }
                        }

                    }
                    evaluationLevel.setLevelContent(evaluationContents);
                    evaluationLevels.add(evaluationLevel);
                }
            }
            evaluationType.setContent(evaluationLevels);
        }
        return evaluationTypeList;
    }

    @Override
    public List<TeacherEvaltionPieEntity> getTeacherEvaltionPie(Map<String, Object> params) {
        return teacherEvaluationChartsDao.getTeacherEvaltionPie(params);
    }

    @Override
    public List<EchartsDate> getTeacherEvaltionBar(Map<String, Object> params) {
        List<EchartsDate> list=new ArrayList<>();
      //  list=teacherEvaluationChartsDao.getEvaltionType();
        list=teacherEvaluationChartsDao.getEvaltionLevel();
        for(int i=0;i<list.size();i++)
        {
            params.remove("levelName");
            params.put("levelName",list.get(i).getName());
            List<String> data=teacherEvaluationChartsDao.getEvaltionCount(params);
            list.get(i).setData(data);
        }
        return list;
    }

    @Override
    public List<TeacherEvaltionPieEntity> getiMatchContentRate(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        PieEntity map= teacherEvaluationChartsDao.getiMatchContentRate(params);
        List<TeacherEvaltionPieEntity> list=new ArrayList<>();
        TeacherEvaltionPieEntity t=new TeacherEvaltionPieEntity();
        TeacherEvaltionPieEntity t1=new TeacherEvaltionPieEntity();
        t.setName("相符");
        t1.setName("不相符");
        if(map==null||map.getMatcha()==null) {
            t.setValue(0.0);
            t1.setValue(0.0);
        }else {
            t.setValue(map.getMatcha());
            Double d=map.getTotal()-map.getMatcha();
            t1.setValue(d);
        }
        list.add(t);
        list.add(t1);
        return list;
    }

    @Override
    public List<EchartsDate> getTeacherEvaltionRateBar(Map<String, Object> params) {
        //????
        return null;
    }

    @Override
    public Map<String, Object> getAverageTeacheingTimeRanking(Map<String, Object> params) {
        String valid=teacherPortraitService.ValidDatesConverter(params);
        //加入肯德基豪华套餐
        params.put("ValidDates",valid);
        Map<String,Object> map=new HashMap<>();
        List<String> classNamesFirstTen=new ArrayList<>();
        List<Integer> rateFirstTen=new ArrayList<>();
        List<String> classNamesEndtTen=new ArrayList<>();
        List<Integer> rateEndTen=new ArrayList<>();
//        if(params.get("startDate").equals(""))
//        {
//            params.remove("startDate");
//            params.put("startDate", DateFormat.getFetureDate(7));
//        }
//        if(params.get("endDate").equals(""))
//        {
//            params.remove("endDate");
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            params.put("endDate", df.format(new Date()));
//        }
        List<AttendenceRate> list=teacherEvaluationChartsDao.getAverageTeacheingTimeRanking(params);
        int j=10;
        if(j>list.size())
        {
            j=list.size();
        }
        for(int i=0;i<j;i++)
        {
            classNamesEndtTen.add(list.get(i).getRateName());
            rateEndTen.add(list.get(i).getRate());
        }
        for(int i=(list.size()-1);i>=(list.size()-j);i--)
        {
            classNamesFirstTen.add(list.get(i).getRateName());
            rateFirstTen.add(list.get(i).getRate());
        }
        Double averageTime=teacherEvaluationChartsDao.getAverageTime();
        map.put("classNamesFirstTen",classNamesFirstTen);
        map.put("rateFirstTen",rateFirstTen);
        map.put("classNamesEndtTen",classNamesEndtTen);
        map.put("rateEndTen",rateEndTen);
        map.put("averageTime",averageTime);
        return map;
    }

    @Override
    public Map<String, Object> getCollegeTeacheingRanking(Map<String, Object> params) {
        Map<String,Object> map=new HashMap<>();
        List<String> collageName=new ArrayList<>();
        List<Integer> collageRate=new ArrayList<>();
        List<String> teachingTeacherTotals=new ArrayList<>();
        List<String> noTeachingTeacherTotals=new ArrayList<>();
//        if(params.get("startDate").equals(""))
//        {
//            params.remove("startDate");
//            params.put("startDate", DateFormat.getFetureDate(7));
//        }
//        if(params.get("endDate").equals(""))
//        {
//            params.remove("endDate");
//            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
//            params.put("endDate", df.format(new Date()));
//        }
        List<TeachingRate> list=teacherEvaluationChartsDao.getCollegeTeacheingRanking(params);
        for(TeachingRate a:list)
        {
            collageName.add(a.getRateName());
            collageRate.add(a.getRate());
            teachingTeacherTotals.add(a.getTeachingTeacherTotal());
            noTeachingTeacherTotals.add(a.getNoTeachingTeacherTotal());
        }
        map.put("collageName",collageName);
        map.put("collageRate",collageRate);
        map.put("teachingTeacherTotals",teachingTeacherTotals);
        map.put("noTeachingTeacherTotals",noTeachingTeacherTotals);
        return map;
    }

    @Override
    public Map<String, Object> getTeacherWorkTimeRate(Map<String, Object> params) {
        Map<String,Object> map=new HashMap<>();
        List<String> collageName=new ArrayList<>();
        List<Integer> collageRate=new ArrayList<>();
        int total= teacherInfoDao.queryAmount(params);
        params.put("total",total);

        List<AttendenceRate> list=teacherEvaluationChartsDao.getTeacherWorkTimeRate(params);
        for(AttendenceRate a:list)
        {
            collageName.add(a.getRateName());
            collageRate.add(a.getRate());
        }
        map.put("name",collageName);
        map.put("rate",collageRate);
        return map;
    }

    @Override
    public List getTeacherWorkEcharts(Map<String, Object> params) {
        List<TeacherEvaltionPieEntity> list=new ArrayList<>();
        int total= teacherInfoDao.queryAmount(params);
        String hasTotal=teacherEvaluationChartsDao.getTeacherWorkEcharts(params);
        Double hasTotal1=Double.parseDouble(hasTotal);
        Double nototal= new Double(total) -hasTotal1;
        TeacherEvaltionPieEntity t=new TeacherEvaltionPieEntity();
        TeacherEvaltionPieEntity t1=new TeacherEvaltionPieEntity();
        t.setName("有课");
        t1.setName("没课");
        t.setValue(hasTotal1);
        t1.setValue(nototal);
        list.add(t);
        list.add(t1);
        return list;
    }
}
